/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.mindlistening.webui;

import com.googlecode.mindlistening.audio.SimpleAudioRecorder;
import com.googlecode.mindlistening.events.ExitApplicationMessage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.bushe.swing.event.EventBus;
import org.bushe.swing.event.EventSubscriber;
import org.codehaus.jackson.map.ObjectMapper;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.MimeTypes;
import org.mortbay.jetty.Request;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.AbstractHandler;
import org.mortbay.jetty.handler.HandlerList;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.proxy.AsyncProxyServlet.Transparent;


/**
 *
 * @author ryan
 */
public class WebUI extends AbstractHandler implements EventSubscriber<ExitApplicationMessage> {



    Server server;
    private String host = "localhost";
    private int port = 5987;
    private Map<String,InternalJsonHandler> internalHandlers;


    public WebUI(Map<String,InternalJsonHandler> internalHandlers) {
        this.internalHandlers = internalHandlers;
    }


    public void startWebUI() throws Exception {

        server = new Server();
        Connector connector = new SelectChannelConnector();
        connector.setPort(getPort());
        connector.setHost(getHost());
        server.addConnector(connector);





        final URL warUrl = WebUI.class.getClassLoader().getResource("html");
        final String warUrlString = warUrl.toExternalForm();
        WebAppContext files = new WebAppContext(warUrlString, "/");
        Map initParams = new HashMap();
        initParams.put("cacheControl", "max-age=3600,public");
        files.setInitParams(initParams);

        files.setMimeTypes(new MimeTypes());
        files.setExtractWAR(false);

        HandlerList handlers = new HandlerList();

        handlers.setHandlers(new Handler[]{this, files});
        server.setHandler(handlers);
        server.start();

        EventBus.subscribeStrongly(ExitApplicationMessage.class, this);








    }

    @Override
    public void onEvent(ExitApplicationMessage t) {
        try {
            server.stop();
        } catch (Exception ex) {
            Logger.getLogger(WebUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void handle(String string, HttpServletRequest request, HttpServletResponse response, int i) throws IOException, ServletException {

        if (string.startsWith("/json")) {
            System.out.println("get: " + string);
            InternalJsonHandler handler = internalHandlers.get(string);
            if (handler != null) {
                handler.handle(string, request, response, i);
            }
        }
    }

    /**
     * @return the host
     */
    public String getHost() {
        return host;
    }

    /**
     * @return the port
     */
    public int getPort() {
        return port;
    }

}
